package manager;

import Database.Connectmysql;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

public class UpdateBook extends JFrame {
    private JLabel lblBookName;
    private JLabel lblAuthor,bookId;
    private JTextField bookName,bookIdField ;
    private JTextField author;
    private JButton addBook;
    public UpdateBook() {
        setTitle("修改图书");
        setBounds(300, 100, 500, 400);
        setLayout(null);
        bookId = new JLabel("书编号");
        lblBookName = new JLabel("书名");
        lblAuthor = new JLabel("作者");
        bookName = new JTextField(30);
        author = new JTextField(30);
        addBook = new JButton("修改");
        bookIdField = new JTextField(30);

        lblBookName.setBounds(80, 30, 200, 30);
        lblAuthor.setBounds(80, 80, 200, 30);
        bookName.setBounds(200, 30, 200, 30);
        author.setBounds(200, 80, 200, 30);
        bookId.setBounds(80,150,200,30);
        bookIdField.setBounds(200, 150,200,30);
        addBook.setBounds(200, 300, 80, 30);

        add(lblBookName);
        add(lblAuthor);
        add(bookName);
        add(author);
        add(addBook);
        add(bookIdField);
        add(bookId);

        addBook.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int bookid = 0;
                int id = Integer.parseInt(bookIdField.getText());
                String bookNameText = bookName.getText();
                String authorText = author.getText();
                LocalDateTime time = LocalDateTime.now();

                Connectmysql cmysql = new Connectmysql(
                        "jdbc:mysql://localhost:3306/bookmanagement",
                        "root",
                        "root",
                        "com.mysql.cj.jdbc.Driver");
                try {
                    if(cmysql.open()){
                        cmysql.executeSqlUpdate("update book set bookname=?, writer=? where bookid=?", new Object[]{bookNameText, authorText, id});
                        System.out.println("修改成功！");
                        setVisible(false);
                    }
                } catch (ClassNotFoundException f) {
                    f.printStackTrace();
                }
            }
        });
        setVisible(true);
    }
    public static void main(String[] args) {
        new UpdateBook().setVisible(true);
    }
}
